1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#![allow(non_camel_case_types, non_snake_case)]

use crate::co;
use crate::decl::*;
use crate::kernel::ffi_types::*;
use crate::ole::privs::*;
use crate::prelude::*;
use crate::vt::*;

/// [`IDXGISwapChain`](crate::IDXGISwapChain) virtual table.
#[repr(C)]
pub struct IDXGISwapChainVT {
	pub IDXGIDeviceSubObjectVT: IDXGIDeviceSubObjectVT,
	pub Present: fn(COMPTR, u32, u32) -> HRES,
	pub GetBuffer: fn(COMPTR, u32, PCVOID, *mut COMPTR) -> HRES,
	pub SetFullscreenState: fn(COMPTR, BOOL, COMPTR) -> HRES,
	pub GetFullscreenState: fn(COMPTR, *mut BOOL, *mut COMPTR) -> HRES,
	pub GetDesc: fn(COMPTR, PVOID) -> HRES,
	pub ResizeBuffers: fn(COMPTR, u32, u32, u32, u32, u32) -> HRES,
	pub ResizeTarget: fn(COMPTR, PCVOID) -> HRES,
	pub GetContainingOutput: fn(COMPTR, *mut COMPTR) -> HRES,
	pub GetFrameStatistics: fn(COMPTR, PVOID) -> HRES,
	pub GetLastPresentCount: fn(COMPTR, *mut u32) -> HRES,
}

com_interface! { IDXGISwapChain: "310d36a0-d2e7-4c0a-aa04-6a9d23b8886a";
	/// [`IDXGISwapChain`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nn-dxgi-idxgiswapchain)
	/// COM interface over [`IDXGISwapChainVT`](crate::vt::IDXGISwapChainVT).
	///
	/// Automatically calls
	/// [`Release`](https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-release)
	/// when the object goes out of scope.
}

impl dxgi_IDXGIObject for IDXGISwapChain {}
impl dxgi_IDXGIDeviceSubObject for IDXGISwapChain {}
impl dxgi_IDXGISwapChain for IDXGISwapChain {}

/// This trait is enabled with the `dxgi` feature, and provides methods for
/// [`IDXGISwapChain`](crate::IDXGISwapChain).
///
/// Prefer importing this trait through the prelude:
///
/// ```no_run
/// use winsafe::prelude::*;
/// ```
pub trait dxgi_IDXGISwapChain: dxgi_IDXGIDeviceSubObject {
	fn_com_interface_get! { GetContainingOutput: IDXGISwapChainVT, IDXGIOutput;
		/// [`IDXGISwapChain::GetContainingOutput`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-getcontainingoutput)
		/// method.
	}

	/// [`IDXGISwapChain::GetDesc`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-getdesc)
	/// method.
	#[must_use]
	fn GetDesc(&self) -> HrResult<DXGI_SWAP_CHAIN_DESC> {
		let mut desc = DXGI_SWAP_CHAIN_DESC::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGISwapChainVT>(self).GetDesc)(
					self.ptr(),
					&mut desc as *mut _ as _,
				)
			},
		).map(|_| desc)
	}

	/// [`IDXGISwapChain::GetFrameStatistics`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-getframestatistics)
	/// method.
	fn GetFrameStatistics(&self) -> HrResult<DXGI_FRAME_STATISTICS> {
		let mut stats = DXGI_FRAME_STATISTICS::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGISwapChainVT>(self).GetDesc)(
					self.ptr(),
					&mut stats as *mut _ as _,
				)
			},
		).map(|_| stats)
	}

	/// [`IDXGISwapChain::GetFullscreenState`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-getfullscreenstate)
	/// method.
	#[must_use]
	fn GetFullscreenState(&self) -> HrResult<(bool, Option<IDXGIOutput>)> {
		let mut fullscreen: BOOL = 0;
		let mut queried = unsafe { IDXGIOutput::null() };

		ok_to_hrresult(
			unsafe {
				(vt::<IDXGISwapChainVT>(self).GetFullscreenState)(
					self.ptr(),
					&mut fullscreen,
					queried.as_mut(),
				)
			},
		).map(|_| (
			fullscreen != 0,
			if queried.ptr().is_null() { None } else { Some(queried) },
		))
	}

	/// [`IDXGISwapChain::GetLastPresentCount`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-getlastpresentcount)
	/// method.
	#[must_use]
	fn GetLastPresentCount(&self) -> HrResult<u32> {
		let mut count = u32::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGISwapChainVT>(self).GetLastPresentCount)(
					self.ptr(),
					&mut count,
				)
			},
		).map(|_| count)
	}

	/// [`IDXGISwapChain::Present`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-present)
	/// method.
	fn Present(&self,
		sync_interval: u32,
		flags: co::DXGI_PRESENT,
	) -> HrResult<()> {
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGISwapChainVT>(self).Present)(
					self.ptr(),
					sync_interval,
					flags.raw(),
				)
			},
		)
	}

	/// [`IDXGISwapChain::ResizeBuffers`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-resizebuffers)
	/// method.
	fn ResizeBuffers(&self,
		buffer_count: u32,
		width: u32,
		height: u32,
		new_format: co::DXGI_FORMAT,
		swap_chain_flags: co::DXGI_SWAP_CHAIN_FLAG,
	) -> HrResult<()>
	{
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGISwapChainVT>(self).ResizeBuffers)(
					self.ptr(),
					buffer_count,
					width,
					height,
					new_format.raw(),
					swap_chain_flags.raw(),
				)
			},
		)
	}

	/// [`IDXGISwapChain::ResizeTarget`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-resizetarget)
	/// method.
	fn ResizeTarget(&self,
		new_target_parameters: &DXGI_MODE_DESC,
	) -> HrResult<()>
	{
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGISwapChainVT>(self).ResizeTarget)(
					self.ptr(),
					new_target_parameters as *const _ as _,
				)
			},
		)
	}

	/// [`IDXGISwapChain::SetFullscreenState`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiswapchain-setfullscreenstate)
	/// method.
	fn SetFullscreenState(&self,
		fullscreen: bool,
		target: Option<&impl dxgi_IDXGIOutput>,
	) -> HrResult<()>
	{
		ok_to_hrresult(
			unsafe {
				(vt::<IDXGISwapChainVT>(self).SetFullscreenState)(
					self.ptr(),
					fullscreen as _,
					target.map_or(std::ptr::null_mut(), |t| t.ptr()),
				)
			},
		)
	}
}